package com.cjx.stars.controller;

import com.cjx.stars.common.MyException;
import com.cjx.stars.common.Response;
import com.cjx.stars.common.ResponseUtils;
import com.cjx.stars.pojo.request.AddBlog;
import com.cjx.stars.pojo.request.BlogUpdateRequest;
import com.cjx.stars.pojo.vo.BlogVo;
import com.cjx.stars.service.BlogLikeService;
import com.cjx.stars.service.BlogService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.cjx.stars.constant.FailMessage.ERROR_PARAMETER_IS_BLACK;
import static com.cjx.stars.constant.FailMessage.PARAMETER_ABNORMAL;

/**
 * @author cjx
 * @description 文章请求
 * @createDate 2024/4/3 19:31
 **/
@RequestMapping("/blog")
@RestController
@Log4j2
public class BlogController {

    @Resource
    private BlogService blogService;
    @Resource
    private BlogLikeService blogLikeService;

    @PostMapping("/add")
    public Response<?> addBlog(AddBlog blog) {
        if (blog == null) {
            return ResponseUtils.fail(ERROR_PARAMETER_IS_BLACK);
        }
        return blogService.addBlog(blog);
    }

    @GetMapping("/list")
    public Response<?> getBlogList(@RequestParam int currentPage, @RequestParam String title) {
        if (currentPage <= 0) {
            return ResponseUtils.fail(PARAMETER_ABNORMAL);
        }
        return blogService.getBlogList(currentPage, title);
    }

    @GetMapping("/{id}")
    public Response<?> getBlogById(@PathVariable("id") Long blogId) {
        if (blogId == null || blogId <= 0) {
            return ResponseUtils.fail(PARAMETER_ABNORMAL);
        }
        BlogVo blog = blogService.getBlogVoById(blogId);
        return ResponseUtils.success(blog);
    }

    @PutMapping("/like/{bolgId}")
    public Response<?> addBlogLike(@PathVariable("bolgId") Long blogId) {
        if (blogId == null || blogId <= 0) {
            return ResponseUtils.fail(PARAMETER_ABNORMAL);
        }
        return blogLikeService.addBlogLike(blogId);
    }

    @DeleteMapping("/{bolgId}")
    public  Response<?> deleteBlog(@PathVariable("bolgId") Long blogId){
        if (blogId == null || blogId <= 0) {
            throw new MyException(PARAMETER_ABNORMAL);
        }
        return blogService.deleteBlog(blogId);
    }

    @PutMapping("/update")
    public Response<?> updateBlog( BlogUpdateRequest blogUpdateRequest) {
        if (blogUpdateRequest == null) {
            throw new MyException(PARAMETER_ABNORMAL);
        }
        return blogService.updateBlog(blogUpdateRequest);
    }

    @GetMapping("/list/my/blog")
    public Response<?> getMyBlogList(@RequestParam int currentPage) {
        if (currentPage <= 0) {
            throw new MyException(PARAMETER_ABNORMAL);
        }
        return blogService.getMyBlogList(currentPage);
    }
}
